<script setup lang="ts">
import { ref, onMounted } from 'vue'
import {
  User,
  Document,
  Avatar,
  Monitor,
  Setting
} from '@element-plus/icons-vue'
import { statisticsApi } from '../api'
import { ElMessage } from 'element-plus'

// 统计数据
const statsData = ref([
  { title: '管理员数量', value: 0, icon: User, color: '#409EFF' },
  { title: '文章数量', value: 0, icon: Document, color: '#67C23A' },
  { title: '会员数量', value: 0, icon: Avatar, color: '#E6A23C' },
  { title: '链接数量', value: 0, icon: Setting, color: '#F56C6C' }
])

// 系统信息
const systemInfo = ref({
  os: '',
  nodeVersion: '',
  pm2Version: '',
  memoryUsage: '',
  diskUsage: '',
  cpuUsage: ''
})

// 获取仪表盘数据
const fetchDashboardData = async () => {
  try {
    const res = await statisticsApi.getDashboardData()
    
    if (res.code === 200) {
      const data = res.data
      
      // 更新统计数据
      statsData.value[0].value = data.adminCount || 0
      statsData.value[1].value = data.articleCount || 0
      statsData.value[2].value = data.memberCount || 0
      statsData.value[3].value = data.linkCount || 0
      
      // 更新系统信息
      if (data.systemInfo) {
        systemInfo.value = {
          os: data.systemInfo.os || '未知',
          nodeVersion: data.systemInfo.nodeVersion || '未知',
          pm2Version: data.systemInfo.pm2Version || '未知',
          memoryUsage: data.systemInfo.memoryUsage || '未知',
          diskUsage: data.systemInfo.diskUsage || '未知',
          cpuUsage: data.systemInfo.cpuUsage || '未知'
        }
      }
    } else {
      ElMessage.error(res.message || '获取仪表盘数据失败')
    }
  } catch (error) {
    console.error('获取仪表盘数据失败:', error)
    ElMessage.error('获取仪表盘数据失败')
  }
}

// 组件挂载时获取数据
onMounted(() => {
  fetchDashboardData()
})
</script>

<template>
  <div class="dashboard-container">
    <el-card class="welcome-card">
      <template #header>
        <div class="card-header">
          <span>欢迎使用后台管理系统</span>
        </div>
      </template>
      <div class="welcome-content">
        <p>这是一个基于 Vue 3 + Element Plus 构建的后台管理系统，提供了系统管理、内容管理、会员管理和系统设置等功能模块。</p>
      </div>
    </el-card>

    <div class="stats-container">
      <el-row :gutter="20">
        <el-col :span="6" v-for="(item, index) in statsData" :key="index">
          <el-card shadow="hover" class="stats-card">
            <div class="stats-item">
              <div class="stats-icon" :style="{ backgroundColor: item.color }">
                <el-icon><component :is="item.icon" /></el-icon>
              </div>
              <div class="stats-info">
                <div class="stats-value">{{ item.value }}</div>
                <div class="stats-title">{{ item.title }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <div class="info-container">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-card class="system-info">
            <template #header>
              <div class="card-header">
                <span>系统信息</span>
              </div>
            </template>
            <el-descriptions :column="1" border>
              <el-descriptions-item label="操作系统">{{ systemInfo.os }}</el-descriptions-item>
              <el-descriptions-item label="Node版本">{{ systemInfo.nodeVersion }}</el-descriptions-item>
              <el-descriptions-item label="PM2版本">{{ systemInfo.pm2Version }}</el-descriptions-item>
              <el-descriptions-item label="内存使用">{{ systemInfo.memoryUsage }}</el-descriptions-item>
              <el-descriptions-item label="磁盘使用">{{ systemInfo.diskUsage }}</el-descriptions-item>
              <el-descriptions-item label="CPU使用">{{ systemInfo.cpuUsage }}</el-descriptions-item>
            </el-descriptions>
          </el-card>
        </el-col>
        <el-col :span="12">
          <el-card class="quick-links">
            <template #header>
              <div class="card-header">
                <span>快捷入口</span>
              </div>
            </template>
            <div class="quick-links-content">
              <el-button type="primary" icon="User" @click="$router.push('/admin')">系统管理</el-button>
              <el-button type="success" icon="Document" @click="$router.push('/article')">内容管理</el-button>
              <el-button type="warning" icon="Avatar" @click="$router.push('/member')">会员管理</el-button>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<style scoped>
.dashboard-container {
  padding: 20px;
}

.welcome-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: bold;
}

.welcome-content {
  line-height: 1.8;
}

.stats-container {
  margin-bottom: 20px;
}

.stats-card {
  height: 100px;
}

.stats-item {
  display: flex;
  align-items: center;
  height: 100%;
}

.stats-icon {
  width: 60px;
  height: 60px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
}

.stats-icon .el-icon {
  font-size: 30px;
  color: #fff;
}

.stats-info {
  flex: 1;
}

.stats-value {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 5px;
}

.stats-title {
  font-size: 14px;
  color: #909399;
}

.info-container {
  margin-bottom: 20px;
}

.system-info, .quick-links {
  height: 100%;
}

.quick-links-content {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.quick-links-content .el-button {
  margin: 0;
}
</style>